<template>
  <div>
    <div class="container-fluid">
      <el-row class="spannel_list" :gutter="10">
        <el-col :sm="6" :xs="12">
          <div class="spannel">
            <div class="left">
              <div class="number">10040</div>
              <div class="title">文章总数</div>
            </div>
            <div class="right">
              <i class="el-icon-document-copy"></i>
            </div>
          </div>
        </el-col>
        <el-col :sm="6" :xs="12">
          <div class="spannel">
            <div class="left">
              <div class="number">1056</div>
              <div class="title">日新增文章数</div>
            </div>
            <div class="right">
              <i class="el-icon-document"></i>
            </div>
          </div>
        </el-col>
        <el-col :sm="6" :xs="12">
          <div class="spannel">
           <div class="left">
              <div class="number">11148</div>
              <div class="title">评论总数</div>
            </div>
            <div class="right">
              <i class="el-icon-chat-line-round"></i>
            </div>
          </div>
        </el-col>
        <el-col :sm="6" :xs="12">
          <div class="spannel last">
            <div class="left">
              <div class="number">420</div>
              <div class="title">日新增评论数</div>
            </div>
            <div class="right">
              <i class="el-icon-chat-dot-square"></i>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="container-fluid">
      <el-row class="curve-pie" :gutter="10">
        <el-col :sm="18" :xs="18">
          <div class="gragh_pannel" id="curve_show"></div>
        </el-col>
        <el-col :sm="6" :xs="6">
          <div class="gragh_pannel" id="pie_show"></div>
        </el-col>
      </el-row>
    </div>

    <div class="container-fluid">
      <div class="column_pannel" id="column_show"></div>
    </div>
  </div>
</template>

<script>
// 模块内用命名导出的, 我把他们再收集起来形成对象, 放到echrats变量里
import * as echarts from 'echarts'
export default {
  name: 'my-home',
  data () {
    return {
         picObj: [
         { value: 300, name: '奇趣事' },
         { value: 100, name: '会生活' },
         { value: 260, name: '爱旅行' },
         { value: 180, name: '趣美味' }
      ]
    }
  },
  mounted () {
    // 面积图
    this.setCurveChartFn()
    // 饼状图
    this.picChartFn()
    // 柱状图
    this.columnChartFn()
  },
  methods: {
    // 设置面积图
    setCurveChartFn () {
      // 基于准备好的dom，初始化echarts实例
      const curveChart = echarts.init(document.getElementById('curve_show'))
      // 绘制面积图表
      // 数据源(模拟后台返回的数据)
      const aListAll = [
        { count: 36, date: '2022-04-13' },
        { count: 52, date: '2022-04-14' },
        { count: 78, date: '2022-04-15' },
        { count: 85, date: '2022-04-16' },
        { count: 65, date: '2022-04-17' },
        { count: 72, date: '2022-04-18' },
        { count: 88, date: '2022-04-19' },
        { count: 64, date: '2022-04-20' },
        { count: 72, date: '2022-04-21' },
        { count: 90, date: '2022-04-22' },
        { count: 96, date: '2022-04-23' },
        { count: 100, date: '2022-04-24' },
        { count: 102, date: '2022-04-25' },
        { count: 110, date: '2022-04-26' },
        { count: 123, date: '2022-04-27' },
        { count: 100, date: '2022-04-28' },
        { count: 132, date: '2022-04-29' },
        { count: 146, date: '2022-04-30' },
        { count: 200, date: '2022-05-01' },
        { count: 180, date: '2022-05-02' },
        { count: 163, date: '2022-05-03' },
        { count: 110, date: '2022-05-04' },
        { count: 80, date: '2022-05-05' },
        { count: 82, date: '2022-05-06' },
        { count: 70, date: '2022-05-07' },
        { count: 65, date: '2022-05-08' },
        { count: 54, date: '2022-05-09' },
        { count: 40, date: '2022-05-10' },
        { count: 45, date: '2022-05-11' },
        { count: 38, date: '2022-05-12' }
      ]

      // 但是图标要求直接给数字的值, 所以要把对象的值取出来形成2个只有值的数组
      const aCount = []
      const aDate = []

      for (let i = 0; i < aListAll.length; i++) {
        aCount.push(aListAll[i].count)
        aDate.push(aListAll[i].date)
      }

      // 面积图的echarts配置项(参考文档复制)
      const chartopt = {
        // 标题组件
        title: {
          show: true, //标题是否显示
          text: '月新增文章数', // 文本标题
          textStyle: {
            fontSize: 16 // 字体大小
          },
          left: 15, // 距离左侧距离
          top: 15
        },
        tooltip: { // 提示框组件
          trigger: 'axis' // 坐标轴触发(鼠标放在坐标轴就能出提示框)
        },
        legend: { // 图例组件(每种颜色的意思)
          data: ['新增文章'], // 图例文字解释(要和series里name对应)
          top: '40' // 距离容器顶部px
        },
        toolbox: { // 工具栏
          show: true, // 要显示
          top: '10',
          right: '30',
          feature: { // 各工具配置项
            mark: { show: true }, // 辅助线开关
            dataView: { show: true, readOnly: false }, // 数据视图工具，可以展现当前图表所用的数据，编辑后可以动态更新。
            magicType: { show: true, type: ['line', 'bar'] }, // 动态类型切换
            restore: { show: true }, // 配置项还原
            saveAsImage: { show: true } // 点击保存下载图片
          }
        },
         xAxis: {
          type: 'category',
          data: aListAll.map(item => item.date),
          offset: 25,
          axisLine: { // x轴轴线
            show: true,
            lineStyle: {
              color: '#d1d1d1', // 轴线颜色
              type: 'dashed', // 虚线
            }
          },
          axisLabel: { // x轴刻度
            show: true,
            color: 'gray'
          }
        },
        yAxis: {
          type: 'value',
          splitNumber: 5,
          splitLine: {
            show: true,
            // 分隔线的样式
            lineStyle: {
              color: '#d1d1d1',
              type: 'dashed'
            }
          }
        },
        // 提示框组件
        tooltip: {
          trigger: 'axis'
        },
        // grid 绘制网格
        grid: {
          left: '10%',
          top: '20%'
        },
        color: [
            {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 1,
              y2: 0,
              colorStops: [
                {
                  offset: 0,
                  color: '#499FEE', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#5D75F0', // 100% 处的颜色
                },
              ],
            },
          ],
          series: [
            {
              data: aListAll.map((item) => item.count),
              type: 'line',
              smooth: true,
              lineStyle: {
                width: 2,
              },
              symbolSize: 3,
              areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#499FEE',
                  },
                  {
                    offset: 1,
                    color: 'rgba(255,255,255,0)',
                  },
                ]),
              },
            },
          ],
      }

      curveChart.setOption(chartopt)
    },
    // 设置饼状图
    picChartFn () {
      const oPie = echarts.init(document.getElementById('pie_show'))

      const oPieopt = {
       // 标题组件
        title: {
          show: true, //标题是否显示
          text: '分类文章数量比', // 文本标题
          textStyle: {
            fontSize: 16 // 字体大小
          },
          left: 15, // 距离左侧距离
          top: 15
        },
        tooltip: {
          trigger: 'item', // 在图形上才会触发提示
          formatter: '{a} <br/>{b} : {c} ({d}%)' // 提示的文字显示的格式
          // a: 系列名
          // b: 数据名
          // c: 数值
          // d: 百分比 (只有饼状图生效)
        },
        color: ['#7dbfff', '#5482fa', '#586ee6', '#6de9bb'], // 系列颜色
        legend: { // 图例组件
         bottom: '15%',
          right:'75',  
          data: ['奇趣事', '会生活', '爱旅行', '趣美味'], // 每个部分对应的数据名(要和series里name对应)
        },
        toolbox: { // 工具箱
          show: true,
          x: 'center',
          bottom: '2%',
          feature: { // 各工具配置项
            mark: { show: true }, // 辅助线开关
            dataView: { show: true, readOnly: false }, // 数据视图工具，可以展现当前图表所用的数据，编辑后可以动态更新。
            magicType: { show: true, type: ['line', 'bar'] }, // 动态类型切换
            restore: { show: true }, // 配置项还原
            saveAsImage: { show: true } // 点击保存下载图片
          }
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            center: ['50%','35%'],
            radius: ['30%', '45%'],
            itemStyle: {
            borderRadius: 5,
             borderColor: '#fff',
             borderWidth: 2
           },
            data: [
              { value: 300, name: '奇趣事' },
              { value: 100, name: '会生活' },
              { value: 260, name: '爱旅行' },
              { value: 180, name: '趣美味' }
            ]
          }
        ]
      }
      oPie.setOption(oPieopt)
    },
    // 设置底部柱状图
    columnChartFn () {
      const oColumn = echarts.init(document.getElementById('column_show'))
      const oColumnopt = {
       // 标题组件
        title: {
          show: true, //标题是否显示
          text: '文章访问量', // 文本标题
          textStyle: {
            fontSize: 16 // 字体大小
          },
          left: 15, // 距离左侧距离
          top: 15
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['奇趣事', '会生活', '爱旅行', '趣美味'],
          top: '40',
          left: '20'
        },
        toolbox: {
          show: true,
          top: '20',
          right: '30',
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        xAxis: [
          {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月'],
            offset: 20,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#d1d1d1',
                type: 'dashed'
              }
            },
            axisLabel: {
              show: true,
              color: 'gray'
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '奇趣事',
            type: 'bar',
            barWidth: 15,
            areaStyle: { type: 'default' },
            itemStyle: {
              color: '#7dbfff'
            },
            data: [800, 708, 920, 1090, 1200]
          },
          {
            name: '会生活',
            type: 'bar',
            barWidth: 15,
            areaStyle: { type: 'default' },
            itemStyle: {
              color: '#5482fa'
            },
            data: [400, 468, 520, 690, 800]
          },
          {
            name: '爱旅行',
            type: 'bar',
            barWidth: 15,
            areaStyle: { type: 'default' },
            itemStyle: {
              color: '#586ee6'
            },
            data: [500, 668, 520, 790, 900]
          },
          {
            name: '趣美味',
            type: 'bar',
            barWidth: 15,
            areaStyle: { type: 'default' },
            itemStyle: {
              color: '#68deb3'
            },
            data: [600, 508, 720, 890, 1000]
          }
        ],
        grid: {
          show: true,
          x: 50,
          x2: 30,
          y: 80,
          height: 260
        },
        
      }
      oColumn.setOption(oColumnopt)
    }
  }
}
</script>

<style lang="less" scoped>
.spannel_list {
  padding-top: 18px;
  margin-bottom: 20px;
  height: 140px;
  background-color: #fff;
}

.spannel {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  height: 100px;
  border-right: 1px solid #e4e6f3;
  background-color: #fff;

  .left {
    .number {
    font-weight: bold;
    font-size: 24px;
  }

  .title {
    font-size: 15px;
    color: #5A607F;
  }
  }

  .right {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50px;
    height: 50px;
    background-color: #ECF2FE;
    border-radius: 50%;

    i {
      font-size: 24px;
      color: #3580ff;
    }
  }

}

.last {
  border: none;
}

.gragh_pannel {
  height: 350px;
  border: 1px solid #e7e7e9;
  background-color: #fff !important;
  margin-bottom: 20px;
}

.column_pannel {
  margin-bottom: 20px;
  height: 400px;
  border: 1px solid #e7e7e9;
  background-color: #fff !important;
}
</style>
